home *** CD-ROM | disk | FTP | other *** search
/ Programming a Multiplayer FPS in DirectX / Programming a Multiplayer FPS in DirectX (Companion CD).iso / DirectX / dxsdk_oct2004.exe / dxsdk.exe / Samples / C++ / Direct3D / ShadowVolume / ShadowVolume.fx < prev    next >
Encoding:
Text File  |  2004-09-27  |  12.9 KB  |  458 lines

  1. //--------------------------------------------------------------------------------------
  2. // File: ShadowVolume.fx
  3. //
  4. // The effect file for the ShadowVolume sample.  
  5. // 
  6. // Copyright (c) Microsoft Corporation. All rights reserved.
  7. //--------------------------------------------------------------------------------------
  8.  
  9.  
  10. #define LIGHT_FALLOFF 1.2f
  11.  
  12.  
  13. //--------------------------------------------------------------------------------------
  14. // Global variables
  15. //--------------------------------------------------------------------------------------
  16. float4   g_vAmbient;                // Ambient light color
  17. float3   g_vLightView;              // View space light position/direction
  18. float4   g_vLightColor;             // Light color
  19. float4   g_vShadowColor;            // Shadow volume color (for visualization)
  20. float4   g_vMatColor;               // Color of the material
  21. float4x4 g_mWorldView;              // World * View matrix
  22. float4x4 g_mProj;                   // Projection matrix
  23. float4x4 g_mWorldViewProjection;    // World * View * Projection matrix
  24. texture  g_txScene;                 // texture for scene rendering
  25. float    g_fFarClip;                // Z of far clip plane
  26.  
  27.  
  28. //-----------------------------------------------------------------------------
  29. // Texture samplers
  30. //-----------------------------------------------------------------------------
  31. sampler g_samScene =
  32. sampler_state
  33. {
  34.     Texture = <g_txScene>;
  35.     MinFilter = Linear;
  36.     MagFilter = Linear;
  37.     MipFilter = Linear;
  38. };
  39.  
  40.  
  41. void VertSceneAmbient( float4 vPos : POSITION,
  42.                        float2 vTex0 : TEXCOORD0,
  43.                        out float4 oPos : POSITION,
  44.                        out float2 oTex0 : TEXCOORD0 )
  45. {
  46.     // Transform the position from object space to homogeneous projection space
  47.     oPos = mul( vPos, g_mWorldViewProjection );
  48.  
  49.     // Just copy the texture coordinate through
  50.     oTex0 = vTex0;
  51. }
  52.  
  53.  
  54. float4 PixSceneAmbient( float2 Tex0 : TEXCOORD0 ) : COLOR0
  55. {
  56.     // Lookup mesh texture and modulate it with material and ambient amount
  57.     return g_vAmbient * tex2D( g_samScene, Tex0 ) * g_vMatColor;
  58. }
  59.  
  60.  
  61. void VertScene1x( float4 vPos : POSITION,
  62.                   float3 vNormal : NORMAL,
  63.                   float2 vTex0 : TEXCOORD0,
  64.                   out float4 oDiffuse : COLOR0,
  65.                   out float4 oPos : POSITION,
  66.                   out float2 oTex0 : TEXCOORD0 )
  67. {
  68.     // Transform the position from object space to homogeneous projection space
  69.     oPos = mul( vPos, g_mWorldViewProjection );
  70.     float4 PosView = mul( vPos, g_mWorldView );
  71.  
  72.     // Compute view space normal
  73.     float3 N = normalize( mul( vNormal, (float3x3)g_mWorldView ) );
  74.  
  75.     float3 InvL = g_vLightView - PosView;
  76.     float LengthSq = dot( InvL, InvL );
  77.  
  78.     InvL = normalize( InvL );
  79.     oDiffuse = saturate( dot( N, InvL ) ) * g_vMatColor * g_vLightColor / LengthSq;
  80.  
  81.     // Just copy the texture coordinate through
  82.     oTex0 = vTex0;
  83. }
  84.  
  85.  
  86. float4 PixScene1x( float4 Diffuse : COLOR0,
  87.                    float2 Tex0 : TEXCOORD0 ) : COLOR0
  88. {
  89.     // Lookup mesh texture and modulate it with diffuse and light color
  90.     return tex2D( g_samScene, Tex0 ) * Diffuse;
  91. //    return float4( tex2D( g_samScene, Tex0 ).xyz, 1.0f ) * Diffuse;
  92. }
  93.  
  94.  
  95. void VertScene( float4 vPos : POSITION,
  96.                 float3 vNormal : NORMAL,
  97.                 float2 vTex0 : TEXCOORD0,
  98.                 out float4 oPos : POSITION,
  99.                 out float4 ViewPos : TEXCOORD0,
  100.                 out float3 ViewNormal : TEXCOORD1,
  101.                 out float2 oTex0 : TEXCOORD2,
  102.                 out float4 oDiffuse : TEXCOORD3 )
  103. {
  104.     // Transform the position from view space to homogeneous projection space
  105.     oPos = mul( vPos, g_mWorldViewProjection );
  106.  
  107.     // Compute view space position
  108.     ViewPos = mul( vPos, g_mWorldView );
  109.  
  110.     // Compute world space normal
  111.     ViewNormal = normalize( mul( vNormal, (float3x3)g_mWorldView ) );
  112.  
  113.     // Modulate material with light to obtain diffuse
  114.     oDiffuse = g_vMatColor * g_vLightColor;
  115.  
  116.     // Just copy the texture coordinate through
  117.     oTex0 = vTex0;
  118. }
  119.  
  120.  
  121. float4 PixScene( float4 ViewPos : TEXCOORD0,
  122.                  float3 ViewNormal : TEXCOORD1,
  123.                  float2 Tex0 : TEXCOORD2,
  124.                  float4 Diffuse : TEXCOORD3 ) : COLOR0
  125. {
  126.     // Pixel to light vector
  127.     float3 L = g_vLightView - ViewPos;
  128.     float LenSq = dot( L, L );
  129.     L = normalize( L );
  130.  
  131.     // Compute lighting amount
  132.     float4 I = saturate( dot( normalize( ViewNormal ), L ) ) * Diffuse *
  133.                (LIGHT_FALLOFF * LIGHT_FALLOFF) / LenSq;
  134.  
  135.     // Lookup mesh texture and modulate it with diffuse
  136.     return float4( tex2D( g_samScene, Tex0 ).xyz, 1.0f ) * I;
  137. }
  138.  
  139.  
  140. void VertShadowVolume( float4 vPos : POSITION,
  141.                        float3 vNormal : NORMAL,
  142.                        out float4 oPos : POSITION )
  143. {
  144.     // Compute view space normal
  145.     float3 N = mul( vNormal, (float3x3)g_mWorldView );
  146.  
  147.     // Obtain view space position
  148.     float4 PosView = mul( vPos, g_mWorldView );
  149.  
  150.     // Light-to-vertex vector in view space
  151.     float3 LightVecView = PosView - g_vLightView;
  152.  
  153.     // Perform reverse vertex extrusion
  154.     // Extrude the vertex away from light if it's facing away from the light.
  155.     if( dot( N, -LightVecView ) < 0.0f )
  156.     {
  157.         if( PosView.z > g_vLightView.z )
  158.             PosView.xyz += LightVecView * ( g_fFarClip - PosView.z ) / LightVecView.z;
  159.         else
  160.             PosView = float4( LightVecView, 0.0f );
  161.  
  162.         // Transform the position from view space to homogeneous projection space
  163.         oPos = mul( PosView, g_mProj );
  164.     } else
  165.         oPos = mul( vPos, g_mWorldViewProjection );
  166. }
  167.  
  168.  
  169. float4 PixShadowVolume() : COLOR0
  170. {
  171.     return float4( g_vShadowColor.xyz, 0.1f );
  172. }
  173.  
  174.  
  175. float4 ShowDirtyStencil() : COLOR0
  176. {
  177.     return g_vShadowColor;
  178. }
  179.  
  180.  
  181. float4 PixComplexity( uniform float4 Color ) : COLOR0
  182. {
  183.     return Color;
  184. }
  185.  
  186.  
  187. //--------------------------------------------------------------------------------------
  188. // Techniques
  189. //--------------------------------------------------------------------------------------
  190. technique RenderSceneAmbient
  191. {
  192.     pass P0
  193.     {
  194.         VertexShader = compile vs_1_1 VertSceneAmbient();
  195.         PixelShader  = compile ps_1_1 PixSceneAmbient();
  196.         StencilEnable = false;
  197.         ZFunc = LessEqual;
  198.     }
  199. }
  200.  
  201.  
  202. technique ShowShadowVolume
  203. {
  204.     pass P0
  205.     {
  206.         VertexShader = compile vs_1_1 VertShadowVolume();
  207.         PixelShader  = compile ps_1_1 PixShadowVolume();
  208.         CullMode = Ccw;
  209.         AlphaBlendEnable = true;
  210.         SrcBlend = SrcAlpha;
  211.         DestBlend = InvSrcAlpha;
  212.         // Disable writing to depth buffer
  213.         ZWriteEnable = false;
  214.         ZFunc = Less;
  215.         // Setup stencil states
  216.         StencilEnable = true;
  217.         StencilRef = 1;
  218.         StencilMask = 0xFFFFFFFF;
  219.         StencilWriteMask = 0xFFFFFFFF;
  220.         StencilFunc = Always;
  221.         StencilZFail = Decr;
  222.         StencilPass = Keep;
  223.     }
  224.     pass P1
  225.     {
  226.         VertexShader = compile vs_1_1 VertShadowVolume();
  227.         PixelShader  = compile ps_1_1 PixShadowVolume();
  228.         CullMode = Cw;
  229.         StencilZFail = Incr;
  230.     }
  231. }
  232.  
  233.  
  234. technique ShowShadowVolume2Sided
  235. {
  236.     pass P0
  237.     {
  238.         VertexShader = compile vs_1_1 VertShadowVolume();
  239.         PixelShader  = compile ps_1_1 PixShadowVolume();
  240.         CullMode = None;
  241.         AlphaBlendEnable = true;
  242.         SrcBlend = SrcAlpha;
  243.         DestBlend = InvSrcAlpha;
  244.         // Disable writing to depth buffer
  245.         ZWriteEnable = false;
  246.         ZFunc = Less;
  247.         // Setup stencil states
  248.         TwoSidedStencilMode = true;
  249.         StencilEnable = true;
  250.         StencilRef = 1;
  251.         StencilMask = 0xFFFFFFFF;
  252.         StencilWriteMask = 0xFFFFFFFF;
  253.         Ccw_StencilFunc = Always;
  254.         Ccw_StencilZFail = Incr;
  255.         Ccw_StencilPass = Keep;
  256.         StencilFunc = Always;
  257.         StencilZFail = Decr;
  258.         StencilPass = Keep;
  259.     }
  260. }
  261.  
  262.  
  263. technique RenderShadowVolume
  264. {
  265.     pass P0
  266.     {
  267.         VertexShader = compile vs_1_1 VertShadowVolume();
  268.         PixelShader  = compile ps_1_1 PixShadowVolume();
  269.         CullMode = Ccw;
  270.         // Disable writing to the frame buffer
  271.         AlphaBlendEnable = true;
  272.         SrcBlend = Zero;
  273.         DestBlend = One;
  274.         // Disable writing to depth buffer
  275.         ZWriteEnable = false;
  276.         ZFunc = Less;
  277.         // Setup stencil states
  278.         StencilEnable = true;
  279.         StencilRef = 1;
  280.         StencilMask = 0xFFFFFFFF;
  281.         StencilWriteMask = 0xFFFFFFFF;
  282.         StencilFunc = Always;
  283.         StencilZFail = Decr;
  284.         StencilPass = Keep;
  285.     }
  286.     pass P1
  287.     {
  288.         VertexShader = compile vs_1_1 VertShadowVolume();
  289.         PixelShader  = compile ps_1_1 PixShadowVolume();
  290.         CullMode = Cw;
  291.         StencilZFail = Incr;
  292.     }
  293. }
  294.  
  295.  
  296. technique RenderShadowVolume2Sided
  297. {
  298.     pass P0
  299.     {
  300.         VertexShader = compile vs_1_1 VertShadowVolume();
  301.         PixelShader  = compile ps_1_1 PixShadowVolume();
  302.         CullMode = None;
  303.         // Disable writing to the frame buffer
  304.         AlphaBlendEnable = true;
  305.         SrcBlend = Zero;
  306.         DestBlend = One;
  307.         // Disable writing to depth buffer
  308.         ZWriteEnable = false;
  309.         ZFunc = Less;
  310.         // Setup stencil states
  311.         TwoSidedStencilMode = true;
  312.         StencilEnable = true;
  313.         StencilRef = 1;
  314.         StencilMask = 0xFFFFFFFF;
  315.         StencilWriteMask = 0xFFFFFFFF;
  316.         Ccw_StencilFunc = Always;
  317.         Ccw_StencilZFail = Incr;
  318.         Ccw_StencilPass = Keep;
  319.         StencilFunc = Always;
  320.         StencilZFail = Decr;
  321.         StencilPass = Keep;
  322.     }
  323. }
  324.  
  325.  
  326. technique RenderShadowVolumeComplexity
  327. {
  328.     pass P0
  329.     {
  330.         VertexShader = compile vs_1_1 VertShadowVolume();
  331.         PixelShader  = compile ps_1_1 PixShadowVolume();
  332.         CullMode = None;
  333.         // Disable writing to the frame buffer
  334.         AlphaBlendEnable = false;
  335.         // Disable writing to depth buffer
  336.         ZWriteEnable = false;
  337.         ZFunc = Less;
  338.         // Setup stencil states
  339.         StencilEnable = true;
  340.         StencilRef = 1;
  341.         StencilMask = 0xFFFFFFFF;
  342.         StencilWriteMask = 0xFFFFFFFF;
  343.         StencilFunc = Always;
  344.         StencilZFail = Incr;
  345.         StencilPass = Incr;
  346.     }
  347. }
  348.  
  349.  
  350. technique RenderScene1x
  351. {
  352.     pass P0
  353.     {
  354.         VertexShader = compile vs_1_1 VertScene1x();
  355.         PixelShader  = compile ps_1_1 PixScene1x();
  356.         ZEnable = true;
  357.         ZFunc = LessEqual;
  358.         StencilEnable = true;
  359.         AlphaBlendEnable = true;
  360.         BlendOp = Add;
  361.         SrcBlend = One;
  362.         DestBlend = One;
  363.         StencilRef = 1;
  364.         StencilFunc = Greater;
  365.         StencilPass = Keep;
  366.     }
  367. }
  368.  
  369.  
  370. technique RenderScene
  371. {
  372.     pass P0
  373.     {
  374.         VertexShader = compile vs_1_1 VertScene();
  375.         PixelShader  = compile ps_2_0 PixScene();
  376.         ZEnable = true;
  377.         ZFunc = LessEqual;
  378.         StencilEnable = true;
  379.         AlphaBlendEnable = true;
  380.         BlendOp = Add;
  381.         SrcBlend = One;
  382.         DestBlend = One;
  383.         StencilRef = 1;
  384.         StencilFunc = Greater;
  385.         StencilPass = Keep;
  386.     }
  387. }
  388.  
  389.  
  390. technique RenderDirtyStencil
  391. {
  392.     pass P0
  393.     {
  394.         VertexShader = compile vs_1_1 VertScene();
  395.         PixelShader  = compile ps_1_1 ShowDirtyStencil();
  396.         ZEnable = false;
  397.         StencilEnable = true;
  398.         AlphaBlendEnable = true;
  399.         SrcBlend = SrcAlpha;
  400.         DestBlend = InvSrcAlpha;
  401.         StencilRef = 0;
  402.         StencilFunc = Less;
  403.         StencilPass = Keep;
  404.     }
  405. }
  406.  
  407.  
  408. technique RenderComplexity
  409. {
  410.     pass p0
  411.     {
  412.         VertexShader = null;
  413.         PixelShader = compile ps_1_1 PixComplexity( float4( 1.0f, 1.0f, 1.0f, 1.0f ) );
  414.         StencilRef = 71;
  415.         ZEnable = false;
  416.         StencilEnable = true;
  417.         AlphaBlendEnable = false;
  418.         StencilFunc = LessEqual;
  419.         StencilPass = Zero;
  420.         StencilFail = Keep;
  421.     }
  422.     pass p1
  423.     {
  424.         PixelShader = compile ps_1_1 PixComplexity( float4( 1.0f, 0.0f, 0.0f, 1.0f ) );
  425.         StencilRef = 51;
  426.     }
  427.     pass p2
  428.     {
  429.         PixelShader = compile ps_1_1 PixComplexity( float4( 1.0f, 0.5f, 0.0f, 1.0f ) );
  430.         StencilRef = 41;
  431.     }
  432.     pass p3
  433.     {
  434.         PixelShader = compile ps_1_1 PixComplexity( float4( 1.0f, 1.0f, 0.0f, 1.0f ) );
  435.         StencilRef = 31;
  436.     }
  437.     pass p4
  438.     {
  439.         PixelShader = compile ps_1_1 PixComplexity( float4( 0.0f, 1.0f, 0.0f, 1.0f ) );
  440.         StencilRef = 21;
  441.     }
  442.     pass p5
  443.     {
  444.         PixelShader = compile ps_1_1 PixComplexity( float4( 0.0f, 1.0f, 1.0f, 1.0f ) );
  445.         StencilRef = 11;
  446.     }
  447.     pass p6
  448.     {
  449.         PixelShader = compile ps_1_1 PixComplexity( float4( 0.0f, 0.0f, 1.0f, 1.0f ) );
  450.         StencilRef = 6;
  451.     }
  452.     pass p7
  453.     {
  454.         PixelShader = compile ps_1_1 PixComplexity( float4( 1.0f, 0.0f, 1.0f, 1.0f ) );
  455.         StencilRef = 1;
  456.     }
  457. }
  458.